package com.sgg.datastructure.queue;


public class ArrayQueue {
    int size;//队列容量
    int[] arr;//数组存放数据
    int front;//队列头指针
    int rear;//队列尾指针

    public ArrayQueue(int size) {
        this.size = size;
        this.front=-1;
        this.rear=-1;
        arr = new int[size];
    }

    public void show(){
        if (isEmpty()) {
            System.out.println("队列空的,无法取数据");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("a[%d]=%d\n",i,arr[i]);
        }
    }

    public boolean isFull(){
        return rear == size - 1;
    }

    public boolean isEmpty(){
        return rear == front;
    }


    public void add(int val) {
        if (isFull()) {
            System.out.println("满了,无法添加");
            return;
        }
        rear++;
        arr[rear] = val;
    }

    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列空的,无法取数据");
        }
        front++;
        return arr[front];
    }

    public int head(){
        if (isEmpty()) {
            throw new RuntimeException("队列空的,无法取数据");
        }
        return arr[front+1];
    }

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        queue.add(3);
        System.out.println(queue.head());
        queue.add(4);
        System.out.println(queue.head());
        queue.add(5);
        System.out.println(queue.head());
        queue.add(6);
        queue.show();
        System.out.println(queue.get());
        System.out.println(queue.get());
        System.out.println(queue.get());
        System.out.println(queue.get());
    }
}
